<?php

namespace app\index\controller;
use think\Db as portraitDB;

class UserAcademicPortrait extends Common{

    public $datas;

    /**
     * 根据条件检索师生学术画像(分页)
     */
    public function searchUserAcademicPortrait(){
        /**
         * userId 用户id,如果登陆了则需要传,没登陆不需要传
         * theme 主题
         * subject 学科
         * ckeyWords 关键词条件(and/or)
         * keyWords 关键词
         * centerSchoolTime 进校时间条件(and/or)
         * enterSchoolTimeStart 进校时间起
         * enterSchoolTimeEnd 进校时间止
         * cprofessionalTitle 职称条件(and/or)
         * professionalTitle 职称
         * csex 性别条件(and/or)
         * sex 性别(1.男 2.女)
         * pageNo 页数
         * pageSize 每页条数
         */
        $this->datas = $this->params;

        try{
            $this->getPersonalPortrait();
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }

    /**
     * 查询个人画像
     */
    private function getPersonalPortrait(){

        $pageNo = 1;
        $pageSize = 10;

        if(!empty($this->datas['pageNo'])){
            $pageNo = $this->datas['pageNo'];
        }

        if(!empty($this->datas['pageSize'])){
            $pageSize = $this->datas['pageSize'];
        }

        $pageNo = ($pageNo - 1) * $pageSize;


        /**
         * 查询条件拼接
         */
        $condition = "";
        //只有关键词传入的时候查user_keywords表
        /*if(!empty($this->datas['keyWords']) && !empty($this->datas['ckeyWords']) && empty($this->datas['theme']) && empty($this->datas['subject']) && empty($this->datas['scholarName'])){
            $sql = "SELECT b.* from (SELECT DISTINCT(kid) FROM user_keywords "
                ." WHERE keyword like '%关节%' ORDER BY count desc) a "
                ." LEFT JOIN scholar b on a.kid = b.kid limit ".$pageNo.",".$pageSize;
            $res = portraitDB::query($sql);
            $countsql = "SELECT COUNT(*) num from (SELECT DISTINCT(kid) FROM user_keywords "
                        ." WHERE keyword like '%关节%' ORDER BY count desc) a "
                        ." LEFT JOIN scholar b on a.kid = b.kid;";
            $count = portraitDB::query($countsql);
        }else{*/
            if(!empty($this->datas['theme'])){
                //$condition .= "and theme like '%".$this->datas['theme']."%' ";
                $condition .= "and (theme like '%".$this->datas['theme']."%' "
                    ." or scholar_name like '%".$this->datas['theme']."%' "
                    ." or institute like '%".$this->datas['theme']."%' "
                    ." or key_words like '%".$this->datas['theme']."%') ";
            }
            if(!empty($this->datas['subject'])){
                $subjectArray = explode(' ',$this->datas['subject']);
                //单个词查询条件拼接
                if(count($subjectArray) <= 1){
                    $condition .= "and subject like '%".$this->datas['subject']."%' ";
                }else{//多歌词查询条件拼接
                    $condition .= "and (";
                    foreach ($subjectArray as $v) {
                        $condition .= " subject like '%".$v."%' or";
                    }
                    $condition = substr($condition,0,strlen($condition)-2);
                    $condition = $condition.") ";
                }
            }
            if(!empty($this->datas['keyWords']) && !empty($this->datas['ckeyWords'])){
                $keyWordsArray = explode(' ',$this->datas['keyWords']);
                //单个词查询条件拼接
                if(count($keyWordsArray) <= 1){
                    $condition .= "and key_words like '%".$this->datas['keyWords']."%' ";
                }else{//多歌词查询条件拼接
                    $condition .= "and (";
                    foreach ($keyWordsArray as $v) {
                        $condition .= " key_words like '%".$v."%' or";
                    }
                    $condition = substr($condition,0,strlen($condition)-2);
                    $condition = $condition.") ";
                }
            }
            if(!empty($this->datas['scholarName'])){
                $condition .= " and scholar_name like '%".$this->datas['scholarName']."%' ";
            }


            $sql = "SELECT id,kid,avatar_path avatarPath,scholar_name scholarName,unit,subject,professional_title professionalTitle,"
                ."key_words keyWords,results_count resultsCount,research_activity researchActivity,cooperation_level cooperationLevel"
                ." FROM scholar where 1=1 ".$condition." limit ".$pageNo.",".$pageSize;
            $res = portraitDB::query($sql);
            $countsql = "SELECT count(1) num FROM scholar where 1=1 ".$condition;
            $count = portraitDB::query($countsql);
        //}

        if(empty($res)){
            $this->returnMsg(1, '暂无数据！');
        }
        /**
         * 如果能搜到数据则添加搜索关键词
         */
        $this->addSearchKeywords();

        $this->returnMsg(1, '获取数据成功！', $res, $count[0]["num"]);
    }

    /**
     * 添加搜索关键词
     */
    private function addSearchKeywords(){
        $insertData = [];
        if(!empty($this->datas['userId'])){
            $insertData['user_id'] = $this->datas['userId'];
        }
        $insertData['word'] = $this->datas['theme'];

        portraitDB::table('search_keywords')->insert($insertData);
    }


    /**
     * 为您推荐
     */
    public function recommendForYou(){
        try{
            $sql = "SELECT id,kid,avatar_path avatarPath,name,institute,unit,key_words keyWords FROM random_recommend, ( SELECT id AS sid FROM random_recommend ORDER BY RAND() LIMIT 3) tmp "
                  ."WHERE random_recommend.id = tmp.sid";
            $res = portraitDB::query($sql);
            if(empty($res)){
                $this->returnMsg(1, '暂无数据！');
            }

            $this->returnMsg(1, '获取数据成功！', $res);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }


    /**
     * 最新动态
     */
    public function latestNews(){
        $pageNo = 1;
        $pageSize = 10;

        if(!empty($this->datas['pageNo'])){
            $pageNo = $this->datas['pageNo'];
        }

        if(!empty($this->datas['pageSize'])){
            $pageSize = $this->datas['pageSize'];
        }

        $pageNo = ($pageNo - 1) * $pageSize;
        try{
            $sql = "SELECT id,creatorAll,resourceCode,LEFT(create_time,10) createTime from resource_collection ORDER BY create_time desc limit ".$pageNo.",".$pageSize;
            $countsql = "SELECT count(1) num FROM resource_collection";
            $count = portraitDB::query($countsql);
            $res = portraitDB::query($sql);

            if(!empty($res)){
                foreach($res as &$value){
                    //查询资源详情
                    $resourceInfo = portraitDB::table('resource_collection')->where('id',$value['id'])->find();
                    //论文：D  I   E   J   U   C
                    //成果奖励:A
                    //图书:B
                    //专利:P
                    //项目（横向，纵向）:O
                    $resourceInfo;
                    switch ($value['resourceCode']) {
                        case "C":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            $value['doi'] = $resourceInfo['doi'];
                            break;
                        case "D":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            $value['doi'] = $resourceInfo['doi'];
                            break;
                        case "I":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            $value['doi'] = $resourceInfo['doi'];
                            break;
                        case "E":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            $value['doi'] = $resourceInfo['doi'];
                            break;
                        case "J":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            $value['doi'] = $resourceInfo['doi'];
                            break;
                        case "U":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            $value['doi'] = $resourceInfo['doi'];
                            break;
                        case "A":
                            $value['title'] = $resourceInfo['achievementName'];
                            $value['allCompletedPeople'] = $resourceInfo['allCompletedPeople'];
                            $value['awardType'] = $resourceInfo['awardType'];
                            $value['awardWinningYear'] = $resourceInfo['awardWinningYear'];
                            $value['awardWinningLevel'] = $resourceInfo['awardWinningLevel'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            $value['doi'] = $resourceInfo['doi'];
                            break;
                        case "B":
                            $value['title'] = $resourceInfo['title'];
                            $value['creator'] = $resourceInfo['creator'];
                            $value['year'] = $resourceInfo['year'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            $value['doi'] = $resourceInfo['doi'];
                            break;
                        case "P":
                            $value['title'] = $resourceInfo['patentName'];
                            $value['patentName'] = $resourceInfo['patentName'];
                            $value['allInventors'] = $resourceInfo['allInventors'];
                            $value['legalState'] = $resourceInfo['legalState'];
                            $value['applicationNo'] = $resourceInfo['applicationNo'];
                            $value['patentNo'] = $resourceInfo['patentNo'];
                            $value['publicationDate'] = $resourceInfo['publicationDate'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            $value['doi'] = $resourceInfo['doi'];
                            break;
                        case "O":
                            $value['title'] = $resourceInfo['projectName'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['projectNo'] = $resourceInfo['instituteAll'];
                            $value['projectFund'] = $resourceInfo['keyword'];
                            $value['projectOrigin'] = $resourceInfo['journal'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            $value['doi'] = $resourceInfo['doi'];
                            break;
                        default:
                            ;
                    }
                }
            }
            if(empty($res)){
                $this->returnMsg(1, '暂无数据！');
            }

            $this->returnMsg(1, '获取数据成功！', $res, $count[0]["num"]);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }


    /**
     * 顶尖成果
     */
    public function topResults(){
        $pageNo = 1;
        $pageSize = 10;

        if(!empty($this->datas['pageNo'])){
            $pageNo = $this->datas['pageNo'];
        }

        if(!empty($this->datas['pageSize'])){
            $pageSize = $this->datas['pageSize'];
        }

        $pageNo = ($pageNo - 1) * $pageSize;

        try{
            $sql = "SELECT id,title,creatorAll,resourceCode,LEFT(create_time,10) createTime from local_resource_collection WHERE level = 'Q1' ORDER BY create_time desc limit ".$pageNo.",".$pageSize;
            $countsql = "SELECT count(1) num FROM resource_collection";
            $count = portraitDB::query($countsql);
            $res = portraitDB::query($sql);

            if(!empty($res)){
                foreach($res as &$value){
                    //查询资源详情
                    $resourceInfo = portraitDB::table('local_resource_collection')->where('id',$value['id'])->find();
                    //论文：D  I   E   J   U   C
                    //成果奖励:A
                    //图书:B
                    //专利:P
                    //项目（横向，纵向）:O
                    $resourceInfo;
                    switch ($value['resourceCode']) {
                        case "C":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            break;
                        case "D":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            break;
                        case "I":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            break;
                        case "E":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            break;
                        case "J":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            break;
                        case "U":
                            $value['title'] = $resourceInfo['title'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['instituteAll'] = $resourceInfo['instituteAll'];
                            $value['keyword'] = $resourceInfo['keyword'];
                            $value['journal'] = $resourceInfo['journal'];
                            $value['year'] = $resourceInfo['year'];
                            $value['volume'] = $resourceInfo['volume'];
                            $value['issue'] = $resourceInfo['issue'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            break;
                        case "A":
                            $value['title'] = $resourceInfo['achievementName'];
                            $value['allCompletedPeople'] = $resourceInfo['allCompletedPeople'];
                            $value['awardType'] = $resourceInfo['awardType'];
                            $value['awardWinningYear'] = $resourceInfo['awardWinningYear'];
                            $value['awardWinningLevel'] = $resourceInfo['awardWinningLevel'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            break;
                        case "B":
                            $value['title'] = $resourceInfo['title'];
                            $value['creator'] = $resourceInfo['creator'];
                            $value['year'] = $resourceInfo['year'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            break;
                        case "P":
                            $value['title'] = $resourceInfo['patentName'];
                            $value['patentName'] = $resourceInfo['patentName'];
                            $value['allInventors'] = $resourceInfo['allInventors'];
                            $value['legalState'] = $resourceInfo['legalState'];
                            $value['applicationNo'] = $resourceInfo['applicationNo'];
                            $value['patentNo'] = $resourceInfo['patentNo'];
                            $value['publicationDate'] = $resourceInfo['publicationDate'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            break;
                        case "O":
                            $value['title'] = $resourceInfo['projectName'];
                            $value['creatorAll'] = $resourceInfo['creatorAll'];
                            $value['projectNo'] = $resourceInfo['instituteAll'];
                            $value['projectFund'] = $resourceInfo['keyword'];
                            $value['projectOrigin'] = $resourceInfo['journal'];
                            $value['tag'] = $resourceInfo['tag'];
                            $value['url'] = $resourceInfo['url'];
                            break;
                        default:
                            ;
                    }
                }
            }
            if(empty($res)){
                $this->returnMsg(1, '暂无数据！');
            }

            $this->returnMsg(1, '获取数据成功！', $res, $count[0]["num"]);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }


    /**
     * 学术画像首页推荐的搜索词
     */
    public function recommendKeywords(){
        try {
            $sql = "SELECT word,count(word) times FROM search_keywords, ( SELECT id AS sid FROM search_keywords ORDER BY RAND() LIMIT 5) tmp"
                ." WHERE search_keywords.id = tmp.sid  GROUP BY word ORDER BY times desc";
            $res = portraitDB::query($sql);
            if(empty($res)){
                $this->returnMsg(1, '暂无数据！');
            }

            $this->returnMsg(1, '获取数据成功！', $res);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }

    /**
     * 查询个人画像详情
     */
    public function getScholarInfo(){
        $this->datas = $this->params;

        try{
            $sql = "SELECT a.id,kid,avatar_path avatarPath,scholar_name scholarName,"
                    ."academic_degree academicDegree,professional_title professionalTitle,"
                    ."isni,a.institute,a.subject,a.phone,a.email,office_location officeLocation,"
                    ."a.user_detail userDetail,IF(b.scholar_id is null,'0','1') isClaim "
                    ."from scholar a LEFT JOIN user b on a.kid = b.scholar_id WHERE kid = '".$this->datas['kid']."'";

            $res = portraitDB::query($sql);
            if(empty($res)){
                $this->returnMsg(1, '暂无数据！');
            }

            $this->returnMsg(1, '获取数据成功！', $res);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }


    /**
     * 查询个人成果水平
     */
    public function getAchievementLevel(){
        $this->datas = $this->params;

        try {
            //已认领情况
            if($this->datas['isClaim'] == 1){
                //成果数量
                $sql = "SELECT COUNT(1) resultSum FROM user_resource a LEFT JOIN user b ON a.user_id = b.id "
                        ."LEFT JOIN local_resource_collection c ON a.resource_id = c.id "
                        ."WHERE b.user_no = '".$this->datas['id']."'";
                $resultSum = portraitDB::query($sql);

                $sql = "SELECT a.resource_code resourceCode,COUNT(a.resource_code) count FROM user_resource a "
                        ."LEFT JOIN user b ON a.user_id = b.id LEFT JOIN local_resource_collection c ON a.resource_id = c.id "
                        ."WHERE b.user_no = '".$this->datas['id']."' GROUP BY a.resource_code";
                $resultOther = portraitDB::query($sql);
                $resultStandard = array_merge($resultSum,$resultOther);


                //级别数量和转让次数
                $sql = "SELECT c.resourceCode,c.level,count(c.`level`) times FROM user_resource a "
                        ."LEFT JOIN user b ON a.user_id = b.id "
                        ."LEFT JOIN local_resource_collection c ON a.resource_id = c.id "
                        ."WHERE b.user_no = '".$this->datas['id']."' and a.resource_code != 'P' and a.resource_code != 'O' "
                        ." and `level` <> '' and `level` is not null "
                        ."GROUP BY c.resourceCode,c.level ORDER BY c.resourceCode";
                $levelResult = portraitDB::query($sql);

                $sql = "SELECT c.resourceCode,sum(c.transferCount) transferCount FROM user_resource a LEFT JOIN user b "
                        ."ON a.user_id = b.id LEFT JOIN local_resource_collection c ON a.resource_id = c.id "
                        ."WHERE b.user_no = '".$this->datas['id']."' and a.resource_code = 'P' GROUP BY c.resourceCode";
                $transferResult = portraitDB::query($sql);

                $sql = "SELECT c.resourceCode,category,count(category) categoryCount FROM user_resource a LEFT JOIN user b "
                        ."ON a.user_id = b.id LEFT JOIN local_resource_collection c ON a.resource_id = c.id "
                        ."and category is not null and category != '' "
                        ."WHERE b.user_no = '".$this->datas['id']."' and a.resource_code = 'O' GROUP BY c.category "
                        ."ORDER BY field(category,'国家级','省部级','厅局级','校级','其他')";
                $categoryResult = portraitDB::query($sql);
                $levelTralsferResult = array_merge($levelResult,$transferResult,$categoryResult);

                //学术生命周期报表
                $sql = "SELECT resume FROM user_org_creator WHERE scholar_id = '".$this->datas['id']."'";
                $resumeResult = portraitDB::query($sql);
                $sql = "SELECT b.year,COUNT(b.resource_code) amount,b.resource_code FROM user a LEFT JOIN "
                        ."user_resource b on a.id = b.user_id WHERE a.user_no = '".$this->datas['id']."' "
                        ."and year > 1900 GROUP BY b.year,b.resource_code ORDER BY b.resource_code,year";
                $resuleYearAmount = portraitDB::query($sql);
                //$academicLifeCycleResult = array_merge($resumeResult,$resuleYearAmount);


                $returnArray = [];
                $returnArray['resultCount'] = $resultStandard;
                $returnArray['levelCount'] = $levelTralsferResult;
                $returnArray['academicLifeCycle'] = $resuleYearAmount;
                $returnArray['resume'] = $resumeResult;

                if(empty($resultStandard) && empty($levelTralsferResult) && empty($academicLifeCycleResult)){
                    $this->returnMsg(1, '暂无数据！');
                }
                $this->returnMsg(1, '获取数据成功！', $returnArray);
            }else{ //未认领情况
                $sql = "SELECT scholar_name scholarName,institute FROM scholar WHERE kid = '".$this->datas['id']."'";
                $scholarInfo = portraitDB::query($sql);
                //成果数量
                $sql = "SELECT COUNT(1) resultSum FROM resource_collection WHERE "
                        ."creator = '".$scholarInfo[0]['scholarName']."' AND instituteStandard LIKE '%".$scholarInfo[0]['institute']."%'";
                $resultSum = portraitDB::query($sql);
                $sql = "SELECT resourceCode,COUNT(resourceCode) count FROM resource_collection "
                        ."WHERE creator = '".$scholarInfo[0]['scholarName']."' AND instituteStandard LIKE '%".$scholarInfo[0]['institute']."%' GROUP BY resourceCode";
                $resultOther = portraitDB::query($sql);
                $resultStandard = array_merge($resultSum,$resultOther);

                //级别数量和转让次数
                $sql = "SELECT resourceCode,level,count(level) times FROM resource_collection "
                        ."WHERE creator = '".$scholarInfo[0]['scholarName']."' AND instituteStandard LIKE '%".$scholarInfo[0]['institute']."%' "
                        ."and resourceCode != 'P' and `level` <> '' and `level` is not null GROUP BY resourceCode,level ORDER BY resourceCode";
                $levelResult = portraitDB::query($sql);

                $sql = "SELECT resourceCode,sum(transferCount) transferCount FROM resource_collection "
                        ."WHERE creator = '".$scholarInfo[0]['scholarName']."' AND instituteStandard LIKE '%".$scholarInfo[0]['institute']."%' "
                        ."and resourceCode = 'P' GROUP BY resourceCode";
                $transferResult = portraitDB::query($sql);
                $levelTralsferResult = array_merge($levelResult,$transferResult);

                //学术生命周期报表
                $sql = "SELECT year,COUNT(resourceCode) amount,resourceCode FROM resource_collection "
                        ."WHERE creator = '".$scholarInfo[0]['scholarName']."' AND instituteStandard LIKE '%".$scholarInfo[0]['institute']."%' "
                        ."GROUP BY year,resourceCode ORDER BY resourceCode,year";
                $resuleYearAmount = portraitDB::query($sql);


                $returnArray = [];
                $returnArray['resultCount'] = $resultStandard;
                $returnArray['levelCount'] = $levelTralsferResult;
                $returnArray['academicLifeCycle'] = $resuleYearAmount;


                if(empty($resultStandard) && empty($levelTralsferResult) && empty($academicLifeCycleResult)){
                    $this->returnMsg(1, '暂无数据！');
                }
                $this->returnMsg(1, '获取数据成功！', $returnArray);
            }
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }


    /**
     * 分页查询学术资源
     */
    public function getScholarResourcePage(){
        $this->datas = $this->params;

        $pageNo = 1;
        $pageSize = 10;

        if(!empty($this->datas['pageNo'])){
            $pageNo = $this->datas['pageNo'];
        }

        if(!empty($this->datas['pageSize'])){
            $pageSize = $this->datas['pageSize'];
        }

        $pageNo = ($pageNo - 1) * $pageSize;


        try {
            //查询条件拼接
            $conditionSql = "";
            if($this->datas['resourceCode'] != 'all') {
                $resourseArray = explode(',', $this->datas['resourceCode']);
                if (count($resourseArray) <= 1) {
                    $conditionSql = " and c.resourceCode = '" . $this->datas['resourceCode'] . "' ";
                } else {//多歌词查询条件拼接
                    $condition = "";
                    foreach ($resourseArray as $v) {
                        $condition .= "'" . $v . "',";
                    }
                    $condition = substr($condition, 0, strlen($condition) - 1);
                    $conditionSql = " and c.resourceCode in (" . $condition . ")";
                }
            }

            //已认领情况
            if($this->datas['isClaim'] == 1){
                //$sql = "SELECT c.title,c.resourceCode,c.publicationDate,c.titleAlt,c.issue,c.volume FROM user_resource a "
                $sql = "SELECT c.* FROM user_resource a "
                    ."LEFT JOIN user b ON a.user_id = b.id LEFT JOIN local_resource_collection c ON a.resource_id = c.id "
                    ."WHERE b.user_no = '".$this->datas['id']."'".$conditionSql." limit ".$pageNo.",".$pageSize;
                $res = portraitDB::query($sql);
                $countsql = "SELECT count(1) num FROM user_resource a "
                    ."LEFT JOIN user b ON a.user_id = b.id LEFT JOIN local_resource_collection c ON a.resource_id = c.id "
                    ."WHERE b.user_no = '".$this->datas['id']."'".$conditionSql;
                $count = portraitDB::query($countsql);

                if(empty($res)){
                    $this->returnMsg(1, '暂无数据！');
                }
                $this->returnMsg(1, '获取数据成功！', $res,$count[0]['num']);
            }else{//未认领情况
                $sql = "SELECT scholar_name scholarName,institute FROM scholar WHERE id = '".$this->datas['id']."'";
                $scholarInfo = portraitDB::query($sql);

                $sql = "SELECT * FROM resource_collection "
                        ."WHERE creator = '".$scholarInfo[0]['scholarName']."' AND instituteStandard LIKE '%".$scholarInfo[0]['institute']."%' "
                        ."limit ".$pageNo.",".$pageSize;
                $res = portraitDB::query($sql);
                $countsql = "SELECT count(1) num FROM resource_collection "
                    ."WHERE creator = '".$scholarInfo[0]['scholarName']."' AND instituteStandard LIKE '%".$scholarInfo[0]['institute']."%' ";
                $count = portraitDB::query($countsql);

                if(empty($res)){
                    $this->returnMsg(1, '暂无数据！');
                }
                $this->returnMsg(1, '获取数据成功！', $res,$count[0]['num']);
            }
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }

    /**
     * 查询相似人才
     */
    public function getScholarRecommend(){
        $this->datas = $this->params;

        try {
            $sql = "SELECT id,kid,scholar_name scholarName,avatar_path avatarPath,institute,key_words keyWords FROM scholar "
                    ." WHERE kid != '".$this->datas['kid']."' "
                    ." and (theme like '%".$this->datas['theme']."%' "
                    ." or scholar_name like '%".$this->datas['theme']."%' "
                    ." or institute like '%".$this->datas['theme']."%' "
                    ." or key_words like '%".$this->datas['theme']."%') ";
            $res = portraitDB::query($sql);

            if(empty($res)){
                $this->returnMsg(1, '暂无数据！');
            }

            $this->returnMsg(1, '获取数据成功！', $res);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }


    /**
     * 查询学脉关系
     */
    public function getElementaryRelationship(){
        $this->datas = $this->params;

        try {
            $sql = "SELECT org_name orgName,org_name_cn orgNameCn,org_name_en orgNameEn,creator,creator_cn creatorCn,creator_en creatorEn,resume,area,key_words keyWords FROM user_org_creator WHERE scholar_id = (SELECT id FROM scholar WHERE kid = '".$this->datas['id']."')";
            $res = portraitDB::query($sql);

            if(empty($res)){
                $this->returnMsg(1, '暂无数据！');
            }

            $this->returnMsg(1, '获取数据成功！', $res);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }


    /**
     * 查询学者研究领域分布
     */
    public function getScholarResearchAreas(){
        $this->datas = $this->params;

        try {
            $areaInfo = portraitDB::table('scholar_research_areas')->where('user_no',$this->datas['userNo'])->order('count','desc')->select();

            if(empty($areaInfo)){
                $this->returnMsg(1, '暂无数据！');
            }

            $this->returnMsg(1, '获取数据成功！', $areaInfo);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }


    /**
     * 查询学者高频词
     */
    public function getScholarHighFrequencyWord(){
        $this->datas = $this->params;

        try {
            $wordInfo = portraitDB::table('scholar_high_frequency_word')->where('user_no',$this->datas['userNo'])->order('count','desc')->select();

            if(empty($wordInfo)){
                $this->returnMsg(1, '暂无数据！');
            }

            $this->returnMsg(1, '获取数据成功！', $wordInfo);
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }

}